#include "widget.h"
#include "ui_widget.h"
#include <QPalette>
#include <QPainter>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setAutoFillBackground(true);

    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, Qt::white);
    setPalette(palette);

    setMinimumSize(512, 256);
    setMaximumSize(512, 256);

    width = size().width();
    height = size().height();

    pix = new QPixmap(width, height);
    pix->fill(Qt::white);

    image.load(":/images/relay.png");

    startX = startY = 0;
    stepX = image.width();
    stepY = image.height();

    drawPix();
    resize(512, 256);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::drawPix()
{
    // 在pixmap上绘制图像
    pix->fill(Qt::white);

    QPainter painter;
    QPen pen(Qt::DotLine);

    for (int i = stepX; i < width; i += stepX)
    {
        painter.begin(pix);
        painter.setPen(pen);
        painter.drawLine(QPoint(i, 0), QPoint(i, height));
        painter.end();
    }

    for (int i = stepY; i < height; i += stepY)
    {
        painter.begin(pix);
        painter.setPen(pen);
        painter.drawLine(QPoint(0, i), QPoint(width, i));
        painter.end();
    }

    painter.begin(pix);
    painter.drawImage(QPoint(startX, startY), image);
    painter.end();
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() == Qt::ControlModifier)
    {
        // 按住ctrl时微调
        switch( event->key() )
        {
        case Qt::Key_Left:
          {
            startX = (startX - 1 < 0) ? startX : startX - 1;
          }
              break;
        case Qt::Key_Right:
          {
            startX = (startX + 1  + image.width() > width) ? startX : startX + 1;
          }
              break;
        case Qt::Key_Up:
          {
            startY = (startY - 1 < 0) ? startY : startY - 1;
          }
              break;
      case Qt::Key_Down:
        {
            startY = (startY + 1 + image.height() > height) ? startY : startY + 1;
        }
            break;
       }
    }
    else
    {
        startX = startX - startX % stepX;
        startY = startY - startY % stepX;

        switch( event->key() )
        {
        case Qt::Key_Left:
          {
            startX = (startX - stepX < 0) ? startX : startX - stepX;
          }
              break;
        case Qt::Key_Right:
          {
            startX = (startX + stepX  + image.width() > width) ? startX : startX + stepX;
          }
              break;
        case Qt::Key_Up:
          {
            startY = (startY - stepY < 0) ? startY : startY - stepY;
          }
              break;
      case Qt::Key_Down:
        {
            startY = (startY + stepY + image.height() > height) ? startY : startY + stepY;
        }
            break;

        case Qt::Key_Home:
          {
              startX = startY = 0;
          }
              break;

        case Qt::Key_End:
          {
            startX = width - image.width();
              startY = height - image.height();
          }
              break;
       }
    }

    drawPix(); // 根据调整好的位置重新在pix上绘图
    update(); // 触发界面重绘
}

void Widget::paintEvent(QPaintEvent *event)
{
    // 将pix绘制在界面上
    QPainter painter;
    painter.begin(this);
    painter.drawPixmap(QPoint(0, 0), *pix);
    painter.end();
}
